package cn.org.donald.framework.controller;

import cn.org.donald.framework.service.SysPermissionService;
import cn.org.donald.framework.service.SysRoleService;
import cn.org.donald.framework.constant.InfoConstant;
import cn.org.donald.framework.constant.TableConstant;
import cn.org.donald.framework.pojo.dto.AddRolePermissionDto;
import cn.org.donald.framework.pojo.vo.ControllerPermissionVo;
import cn.org.donald.framework.pojo.vo.HttpResult;
import cn.org.donald.framework.pojo.vo.TableResult;
import cn.org.donald.framework.util.ListUtil;
import cn.org.donald.pojo.entity.RolePermission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ： Donald
 * @date ： 2020/11/5 20:53
 * @description： 角色管理
 */
@RestController
@RequestMapping("/sysPermission")
@Api(value = "权限管理",description = "角色权限管理相关接口")
public class SysPermissionController extends BaseController {

    @Autowired
    private SysPermissionService sysPermissionService;

    @Autowired
    private SysRoleService sysRoleService;

    @GetMapping("/getByRoleId")
    @ApiOperation(value = "获取角色已拥有的权限集合")
    @RequiresPermissions("sysPermission:getByRoleId:get")
    public TableResult getByRoleId( Long id){
        List<ControllerPermissionVo> controllerPermissionVos = sysPermissionService.findTreeByRoleId(id);
        return toTableResult(controllerPermissionVos);
    }

    @GetMapping("/getByNotRoleId")
    @ApiOperation(value = "获取角色未拥有的角色集合")
    @RequiresPermissions("sysPermission:getByNotRoleId:get")
    public TableResult getByNotRoleId(Long id){
        List<ControllerPermissionVo> controllerPermissionVos = sysPermissionService.findTreeNotByRoleId(id);
        return toTableResult(controllerPermissionVos);
    }

    @GetMapping("/getTreeByRoleId")
    @ApiOperation(value = "获取角色已拥有的树形权限集合")
    @RequiresPermissions("sysPermission:getTreeByRoleId:get")
    public TableResult getTreeByRoleId(Long id){
        List<ControllerPermissionVo> tree = sysPermissionService.findTree(id);
        if (tree.get(0)!=null){
            tree = tree.stream().filter(controllerPermissionVo -> !CollectionUtils.isEmpty(controllerPermissionVo.getPermissionList())).collect(Collectors.toList());
        }
        return toTableResult(tree);
    }

    @PostMapping("/addRolePermission")
    @ApiOperation(value = "给角色批量分配权限")
    @RequiresPermissions("sysPermission:addRolePermission:post")
    public HttpResult addRolePermission(@RequestBody AddRolePermissionDto addRolePermissionDto){
        if (addRolePermissionDto.getRoleName().equals(TableConstant.VALUE_ADMIN_NAME)){
            return toResultFail("系统超级管理角色不能修改");
        }

        if (ListUtil.isEmptyOrHasClassFieldAllEmpty(addRolePermissionDto.getPermissionList())){
            return toResultFail(InfoConstant.ADD_FAIL);
        }
        int rows = sysPermissionService.insertRolePermissionBatch(addRolePermissionDto);
        return rows>0?toResultSuccess(InfoConstant.ADD_SUCCESS):toResultFail(InfoConstant.ADD_SUCCESS);
    }

    @PostMapping("/deleteRolePermission")
    @ApiOperation(value = "移除已分配的权限")
    @RequiresPermissions("sysPermission:deleteRolePermission:post")
    public HttpResult deleteRolePermission(@RequestBody RolePermission rolePermission){
        if (sysRoleService.isAdmin(rolePermission.getRoleId())){
            return toResultFail(InfoConstant.ADMIN_ROLE_EXCEPT_INFO);
        }
        if (rolePermission.getRoleId()==null||rolePermission.getPermissionName()==null){
            return toResultFail("没有可移除的权限");
        }
        int rows = sysPermissionService.deleteRolePermission(rolePermission);
        return rows>0? toResultSuccess(InfoConstant.REMOVE_SUCCESS):toResultFail(InfoConstant.REMOVE_FAIL);
    }

}
